/**
 * Contains functions to deal with IP packets
 */

#ifndef SR_IP_H_S7LOIHKI
#define SR_IP_H_S7LOIHKI

#include "sr_router.h"
#include "sr_rt.h"


/**
 * Checks what type of IP packet and calls corresponding handler functions.
 *
 * if TTL == 0: send time exceeded ICMP error to sender
 * if destination IP is router:
 *      if ICMP: handle icmp packet
 *      if UDP/TCP: send protocol unreachable to sender
 * else if destination IP not router:
 *      handle_ip_forwarding()
 */
void demultiplex_ip_packet(struct sr_instance* sr, 
                           uint8_t * packet/* lent */,
                           unsigned int len,
                           char* interface/* lent */);

/**
 * Attempts to find next hop in routing table.
 *      if next hop exists: forward
 *      if not in routing table: send ICMP destination unreachable to sender (type 3, code 1)
 */                           
void handle_ip_forwarding( struct sr_instance* sr, 
                            uint8_t * packet/* lent */,
                            unsigned int len,
                            char* interface/* lent */);

/**
 * Finds longest prefix match from routing table and returns sr_rt * with longest 
 * prefix match.
 * If no match found, return NULL.
 */
struct sr_rt *get_longest_prefix_match(struct sr_rt *rtable, uint32_t dest_ip);

#endif /* end of include guard: SR_IP_H_S7LOIHKI */